use lib "../";

use Test::More tests => 12;

Test::More->builder->output ('result.txt');
Test::More->builder->failure_output ('errors.txt');

chdir("..");

subtest 'Integration Test 1' => sub {
	plan tests => 1;
#----------------------------------------------------------------------------------------
#              Verify that the build will fail if the first build is a cron
#----------------------------------------------------------------------------------------	
	my $number = system("perl fw_build.pl --type cron --conf Debug --project playground --rel_inc inc4");
	isnt($number,0);
};

#base1.1
subtest 'Integration Test 2' => sub {
#----------------------------------------------------------------------------------------
#                          Verify that first build is a baseline
#----------------------------------------------------------------------------------------	
	plan tests => 1;
	my $number = system("perl fw_build.pl --type baseline --conf Debug --project playground --rel_inc inc4 --base_open_wi_allow --base_warn_suppress ");
	is($number,0);
};

subtest 'Integration Test 3' => sub {
	plan tests => 1;
#----------------------------------------------------------------------------------------
#                          Verify wrong type parameter in the command line
#----------------------------------------------------------------------------------------
	open(CMD,"perl fw_build.pl --type not_exist --conf Debug --project playground --rel_inc inc1 -- |");
	my @lines = <CMD>;
	open(FILE,">it_3.txt");
	print FILE @lines;
	close FILE;
	close CMD;
	is($lines[0],"For help: fw_build.pl --help\n");
	system("rm -rf it_3.txt");
};

subtest 'Integration Test 4' => sub {
	plan tests => 1;
#----------------------------------------------------------------------------------------
#                          Verify wrong conf parameter in the command line
#----------------------------------------------------------------------------------------
	my $number = system("perl fw_build.pl --type baseline --conf not_exist --rel_inc inc1 --project playground --base_open_wi --base_warn_suppress ");
	isnt($number,0);
};

subtest 'Integration Test 5' => sub {
	plan tests => 2;
#----------------------------------------------------------------------------------------
#                          Verify wrong project 
#----------------------------------------------------------------------------------------
    my $number = system("perl fw_build.pl --type baseline --conf Debug --project doesnt_exist --rel_inc inc1 --base_open_wi_allow --base_warn_suppress ");
	isnt($number,0);
#----------------------------------------------------------------------------------------
#                          Verify wrong rel_inc 
#----------------------------------------------------------------------------------------
    my $number = system("perl fw_build.pl --type baseline --conf Debug --project playground --rel_inc not_exist --base_open_wi_allow --base_warn_suppress ");
	isnt($number,0);
};


subtest 'Integration Test 6' => sub {
	plan tests => 1;
#----------------------------------------------------------------------------------------
#                          Verify missing parameter
#----------------------------------------------------------------------------------------
	open(CMD,"perl fw_build.pl --type baseline --conf Debug |");
	my @lines = <CMD>;
	open(FILE,">it_6.txt");
	print FILE @lines;
	close FILE;
	close CMD;
	is($lines[0],"For help: fw_build.pl --help\n");
	system("rm -rf it_6.txt");
};

#base2.1
subtest 'Integration Test 7/12/14' => sub {
	plan tests => 1;
#----------------------------------------------------------------------------------------
#                          Verify Debug
#----------------------------------------------------------------------------------------
	my $number = system("perl fw_build.pl --type baseline --conf Debug --project playground --rel_inc inc1 --base_open_wi_allow --base_warn_suppress");
	is($number,0);
};

#base3.1
subtest 'Integration Test 8/12/14' => sub {
	plan tests => 1;
#----------------------------------------------------------------------------------------
#                          Verify Release
#----------------------------------------------------------------------------------------
	my $number = system("perl fw_build.pl --type baseline --conf Release --project playground --rel_inc inc1 --base_open_wi_allow --base_warn_suppress");
	is($number,0);
};

#base4.1
subtest 'Integration Test 9/12/14' => sub {
	plan tests => 1;
#----------------------------------------------------------------------------------------
#                          Verify ReleaseRTLx
#----------------------------------------------------------------------------------------
	my $number = system("perl fw_build.pl --type baseline --conf ReleaseRTLx --project playground --rel_inc inc1 --base_open_wi_allow --base_warn_suppress");
	is($number,0);
};

#cron4.2
subtest 'Integration Test 10/15/16' => sub {
	plan tests => 3;
#----------------------------------------------------------------------------------------
#                          Verify All except
#----------------------------------------------------------------------------------------
 	my $number = system("perl fw_build.pl --type cron --conf all_except_Debug_and_Release --project playground --rel_inc inc1");
	is($number,0);
	is(-e "BuildTool/trunk/quals/main_fw/fw_builds/ReleaseRTLx","1", "Verifying that the ReleaseRTLx exists");
#--------------------------------------------------------------------------------------------------------------------------------------
#                          Verify that build fails for two equal consecutive crons and verifying that is not case sensitive with the "all"
#---------------------------------------------------------------------------------------------------------------------------------------
	$number = system("perl fw_build.pl --type cron --conf All_except_Debug_and_Release --project playground --rel_inc inc1");
	isnt($number,0);
	
	
	};

#base5.1
subtest 'Integration Test 11' => sub {
	plan tests => 1;
#----------------------------------------------------------------------------------------
#                          Verify baseline with base_open_wi_allow flag disabled
#----------------------------------------------------------------------------------------
    my $number = system("perl fw_build.pl --type baseline --conf Debug --project playground --rel_inc inc1 ");
	isnt($number,0);
};

subtest 'Integration Test 17' => sub {
	plan tests => 7;
#-------------------------------------------------------------------------------------------------------
#                          Verify that the build doesn't fail for a baseline equal to the previous cron.
#------------------------------------------------------------------------------------------------------
    my $number = system("perl fw_build.pl --type baseline --conf Debug --project playground --rel_inc inc1 --base_open_wi_allow --base_warn_suppress");
	is($number,0);
	system("rm -rf tags");
	system("svn export http://cad-test/repo/crdm/device/firmware/tools/build_tool/tags");
	open(FILE,"<tags/release_history.txt");
	my @line = <FILE>;
	is($line[0],"base5.1\n");
	is($line[1],"cron4.2\n");
	is($line[2],"base4.1\n");
	is($line[3],"base3.1\n");
	is($line[4],"base2.1\n");
	is($line[5],"base1.1\n");
	system("svn delete http://cad-test/repo/crdm/device/firmware/tools/build_tool/tags/release_history.txt http://cad-test/repo/crdm/device/firmware/tools/build_tool/tags/base1.1/ http://cad-test/repo/crdm/device/firmware/tools/build_tool/tags/base2.1/ http://cad-test/repo/crdm/device/firmware/tools/build_tool/tags/base3.1/ http://cad-test/repo/crdm/device/firmware/tools/build_tool/tags/base4.1/ http://cad-test/repo/crdm/device/firmware/tools/build_tool/tags/cron4.2/ http://cad-test/repo/crdm/device/firmware/tools/build_tool/tags/base5.1/ -m \"deleting tested files - automated test\"");
};


#Get date and time and then print them
my ( $sec, $min, $hour, $mday, $mon, $year, $wday, $yday, $isdst ) = localtime(time);
my $month = ("January","February","March","April", "May", "June","July", "August",
		"September", "October",  "November", "December")[$mon];
my $timestamp = sprintf "$month\-%02d,%04d-%02d:%02d", $mday, $year + 1900,$hour, $min;

#Old file
my $test_case_rel = "<integration_tests/result.txt";
my $test_case_err = "<integration_tests/errors.txt";
#Updated file
my $report = ">>integration_tests/$timestamp.txt";
open(TC_R, $test_case_rel);
open(TC_E, $test_case_err);	
open(REPORT, $report);
my @lines;
print REPORT "INTEGRATION TESTS \n";
print FILE "RUN TIME: $timestamp\n";
print REPORT "\n ERRORS: \n\n";
print REPORT @lines;
#Copy the rest of the lines from old file
@lines = <TC_E>;
print REPORT @lines;
@lines = <TC_R>;
print REPORT "\n RESULT: \n\n";
print REPORT @lines;
close REPORT;
close TC_E;
close TC_R;

system("rm -rf integration_tests/result.txt");
system("rm -rf integration_tests/errors.txt");	
system("rm -rf integration_tests/BuildTool");
